package dk.ucn.datamatiker.jmaweather;

import java.util.ArrayList;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class WeatherActivity extends FragmentActivity implements ActionBar.TabListener
{

	ViewPager _viewPager;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_weather);
		
		// Set up the action bar.
		final ActionBar actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		SectionsAdapter pageAdapter = new SectionsAdapter(getSupportFragmentManager());

		// Set up the ViewPager with the sections adapter.
		_viewPager = (ViewPager) findViewById(R.id.pager);
		_viewPager.setAdapter(pageAdapter);

		// When swiping between different sections, select the corresponding
		// tab. We can also use ActionBar.Tab#select() to do this if we have
		// a reference to the Tab.
		_viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
		{
			@Override
			public void onPageSelected(int position)
			{
				actionBar.setSelectedNavigationItem(position);
			}
		});

		pageAdapter.addItem(getString(R.string.tab_local_text), new LocalWeatherFragment());
		pageAdapter.addItem(getString(R.string.tab_global_text), new GlobalWeatherFragment());

		// For each of the sections in the app, add a tab to the action bar.
		for (int i = 0; i < pageAdapter.getCount(); i++)
		{
			actionBar.addTab(actionBar.newTab().setText(pageAdapter.getPageTitle(i)).setTabListener(this));
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.weather, menu);
		return true;
	}

	@Override
	public void onTabSelected(Tab tab, android.app.FragmentTransaction ft)
	{
		_viewPager.setCurrentItem(tab.getPosition());
	}

	@Override
	public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft)
	{
	}

	@Override
	public void onTabReselected(Tab tab, android.app.FragmentTransaction ft)
	{
	}

	public class SectionsAdapter extends FragmentPagerAdapter
	{

		private ArrayList<Fragment> _sections;
		private ArrayList<String> _sectionNames;

		public SectionsAdapter(FragmentManager fm)
		{
			super(fm);
			_sections = new ArrayList<Fragment>();
			_sectionNames = new ArrayList<String>();
		}

		public void addItem(String name, Fragment item)
		{
			_sections.add(item);
			_sectionNames.add(name);
		}

		@Override
		public Fragment getItem(int position)
		{
			return _sections.get(position);
		}

		@Override
		public int getCount()
		{
			return _sections.size();
		}

		@Override
		public String getPageTitle(int position)
		{
			return _sectionNames.get(position);
		}
	}

}
